package com.demo.java8.refactor;

import java.io.Serializable;

/**
 * Created by churcy on 2018/3/26.
 */
public class CommonResult implements Serializable {

    /**
     *
     */
    private String code;
    private String msg;
    private String subCode;
    private String subMsg;
    private String errorCode;
    private String errorMsg;
    private Object data;

    /**
     *
     */
    private CommonResult() {

    }

    private CommonResult(Builder builder) {
        this.code = builder.code;
        this.msg = builder.msg;
        this.errorCode = builder.errorCode;
        this.errorMsg = builder.errorMsg;
        this.data = builder.data;
    }

    /**
     * 构造器，组件返回消息内容
     */
    public static class Builder {

        private String code;
        private String msg;
        private String errorCode;
        private String errorMsg;
        private Object data;

        public Builder code(String code) {
            this.code = code;
            return this;
        }

        public Builder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public Builder errorCode(String errorCode) {
            this.errorCode = errorCode;
            return this;
        }

        public Builder errorMsg(String errorMsg) {
            this.errorMsg = errorMsg;
            return this;
        }

        public Builder data(Object data) {
            this.data = data;
            return this;
        }

        public CommonResult build() {
            return new CommonResult(this);
        }

    }

    /**
     * 返回码和返回消息枚举,主要是为了提供统一的返回码
     */
    public enum CodeAndMsg {

        SUCCESS("10000", "请求成功", null, null),
        FAIL("40000", "请求失败", null, null),
        SYS_ERROR(null, null, "90000", "系统异常"),
        SQL_ERROR(null, null, "90001", "SQL异常"),
        NULL_ERROR(null, null, "90002", "空指针异常");

        private String code;
        private String msg;

        private String errorCode;
        private String errorMsg;

        CodeAndMsg() {

        }

        CodeAndMsg(String code, String msg, String errorCode, String errorMsg) {
            this.code = code;
            this.msg = msg;
            this.errorCode = errorCode;
            this.errorMsg = errorMsg;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getmsg() {
            return msg;
        }

        public void setmsg(String msg) {
            this.msg = msg;
        }

        public String getErrorCode() {
            return errorCode;
        }

        public void setErrorCode(String errorCode) {
            this.errorCode = errorCode;
        }

        public String geterrorMsg() {
            return errorMsg;
        }

        public void seterrorMsg(String errorMsg) {
            this.errorMsg = errorMsg;
        }

    }

    public static Builder getBuilder() {
        return new CommonResult.Builder();
    }

    /**
     * 默认成功返回
     *
     * @return
     */
    public static CommonResult successResult() {
        return new CommonResult.Builder()
                .code(CodeAndMsg.SUCCESS.getCode())
                .msg(CodeAndMsg.SUCCESS.getmsg())
                .build();
    }

    /**
     * 默认成功返回
     *
     * @return
     */
    public static CommonResult successResult(Object data) {
        return new Builder()
                .code(CodeAndMsg.SUCCESS.getCode())
                .msg(CodeAndMsg.SUCCESS.getmsg())
                .data(data)
                .build();
    }

    /**
     * 默认错误返回
     *
     * @return
     */
    public static CommonResult failResult() {
        return new Builder()
                .code(CodeAndMsg.FAIL.getCode())
                .msg(CodeAndMsg.FAIL.getmsg())
                .build();
    }

    /**
     * 默认返回,无返回数据
     *
     * @return
     */
    public static CommonResult commonResult(CodeAndMsg CodeAndMsg) {
        return new Builder()
                .code(CodeAndMsg.getCode())
                .msg(CodeAndMsg.getmsg())
                .errorCode(CodeAndMsg.getErrorCode())
                .errorMsg(CodeAndMsg.geterrorMsg())
                .build();
    }

    /**
     * 默认返回,有数据
     *
     * @return
     */
    public static CommonResult commonResult(CodeAndMsg CodeAndMsg, Object data) {
        return new Builder()
                .code(CodeAndMsg.getCode())
                .msg(CodeAndMsg.getmsg())
                .errorCode(CodeAndMsg.getErrorCode())
                .errorMsg(CodeAndMsg.geterrorMsg())
                .data(data)
                .build();
    }

}
